package cn.manjuu.touhou.activity.fragment;

import cn.manjuu.touhou.APP;
import cn.manjuu.touhou.R;
import cn.manjuu.touhou.engine.BattleEngine;
import cn.manjuu.touhou.interfaces.OnBaseInfoChangeListener;
import cn.manjuu.touhou.util.Looger;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MakeMoneyFragment extends Fragment implements OnClickListener {

	private RelativeLayout mMainView;
	private Button btn_attack;
	private Button btn_defend;
	private Button btn_recover;
	private ProgressBar pb_enemy_hp;
	private ProgressBar pb_left_time;
	private ProgressBar pb_player_hp;
	private int enemyHp;
	private int playerHp;
	private int leftTime;
	private OnBaseInfoChangeListener mListener;

	public ProgressBar getPb_player_hp() {
		return pb_player_hp;
	}

	public ProgressBar getPb_enemy_hp() {
		return pb_enemy_hp;
	}

	public ProgressBar getPb_left_time() {
		return pb_left_time;
	}

	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		super.onAttach(activity);
		try {
			mListener = (OnBaseInfoChangeListener) activity;
		} catch (ClassCastException e) {
			throw new ClassCastException(activity.toString()
					+ "must implement OnBaseInfoChangeListener");
		}
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		mMainView = (RelativeLayout) inflater
				.inflate(R.layout.make_money, null);
		findView();
		registerListener();
		return mMainView;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		init();
	}

	private void init() {
		// TODO Auto-generated method stub
		enemyHp = 5;
		playerHp = 5;
		leftTime = 40;

		pb_left_time.setMax(40);
		pb_left_time.setProgress(leftTime);
		pb_enemy_hp.setMax(10);
		pb_enemy_hp.setProgress(enemyHp);
		pb_player_hp.setMax(10);
		pb_player_hp.setProgress(playerHp);
	}

	@Override
	public void onStart() {
		// TODO Auto-generated method stub
		Looger.i(APP.MakeMoneyFragment, "MakeMoneyFragment onStart");
		super.onStart();
	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		Looger.i(APP.MakeMoneyFragment, "MakeMoneyFragment onResume");
		super.onResume();
		BattleEngine.getInstance().setFragment(this);
		BattleEngine.getInstance().setListener(mListener);
		BattleEngine.getInstance().startBattle();
	}

	private void findView() {
		btn_attack = (Button) mMainView.findViewById(R.id.btn_attack);
		btn_defend = (Button) mMainView.findViewById(R.id.btn_defend);
		btn_recover = (Button) mMainView.findViewById(R.id.btn_recover);
		pb_player_hp = (ProgressBar) mMainView
				.findViewById(R.id.pb_make_money_player_hp);
		pb_enemy_hp = (ProgressBar) mMainView
				.findViewById(R.id.pb_make_money_enemy_hp);
		pb_left_time = (ProgressBar) mMainView
				.findViewById(R.id.pb_make_money_left_time);
	}

	private void registerListener() {
		// TODO Auto-generated method stub
		btn_attack.setOnClickListener(this);
		btn_defend.setOnClickListener(this);
		btn_recover.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btn_attack:
			BattleEngine.getInstance().changePlayerState(BattleEngine.ATTACK);
			break;
		case R.id.btn_defend:
			BattleEngine.getInstance().changePlayerState(BattleEngine.DEFEND);
			break;
		case R.id.btn_recover:
			BattleEngine.getInstance().changePlayerState(BattleEngine.RECOVER);
			break;
		}
	}

	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		Looger.i(APP.MakeMoneyFragment, "MakeMoneyFragment onPause");
		BattleEngine.getInstance().shutdown();
		super.onPause();
	}
}
